package com.shengcheng.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.esotericsoftware.kryo.serializers.TaggedFieldSerializer;
import com.google.common.hash.BloomFilter;
import com.shengcheng.entity.AlbumInfo;
import com.shengcheng.entity.BaseEntity;
import com.shengcheng.service.AlbumInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.experimental.Accessors;
import org.redisson.api.RBloomFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Tag(name = "并发管理接口")
@RestController
@RequestMapping(value = "/api/album")
public class ConcurentController {

    @Autowired
    private AlbumInfoService albumInfoService;

    @Autowired
    private RBloomFilter bloomFilter;

    @Operation(summary = "初始化")
    @GetMapping("init")
    public String initAlbumBloom(){
        //查询数据库里专辑id
        LambdaQueryWrapper<AlbumInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(BaseEntity::getId);
        List<AlbumInfo> albumInfos = albumInfoService.list(wrapper);
        //将id放到布隆过滤器中
        for (AlbumInfo albumInfo : albumInfos) {
            Long id = albumInfo.getId();
            bloomFilter.add(id);
        }

        return "success";
    }
}
